package com.vim.modules.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vim.modules.utils.HttpClientUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;

@RestController
@RequestMapping("/api")
public class ApiController {

    @RequestMapping("/bookRecommend")
    public JSONObject bookRecommend(@RequestParam(value = "id") String id){
        JSONObject result = new JSONObject();
        try {
            String data = HttpClientUtils.sendGetData("http://api.zhuishushenqi.com/recommendPage/node/books/all/"+id+"?st=1&size=6", "utf-8");
            result.put("returnCode", "SUCCESS");
            result.put("data", JSONObject.parse(data));
        }catch (Exception e){
            e.printStackTrace();
            result.put("returnCode", "FAIL");
        }
        return result;
    }

    @RequestMapping("/bookClassifyList")
    public JSONObject bookClassifyList(@RequestParam(value = "classifyName") String classifyName,
                                        @RequestParam(value = "pageNo") String pageNo){
        JSONObject result = new JSONObject();
        try {
            classifyName = URLEncoder.encode(classifyName,"UTF-8");
            Integer start = Integer.valueOf(pageNo)*10;
            String data = HttpClientUtils.sendGetData("http://api.zhuishushenqi.com/book/by-categories?gender=press&type=&major="+classifyName+"&minor=&start="+start.toString()+"&limit=10", "utf-8");
            result.put("returnCode", "SUCCESS");
            result.put("data", JSONObject.parse(data));
        }catch (Exception e){
            e.printStackTrace();
            result.put("returnCode", "FAIL");
        }
        return result;
    }

    @RequestMapping("/bookDetail")
    public JSONObject bookDetail(@RequestParam(value = "id") String id){
        JSONObject result = new JSONObject();
        try {
            String data = HttpClientUtils.sendGetData("http://api.zhuishushenqi.com/book/"+id, "utf-8");
            result.put("returnCode", "SUCCESS");
            result.put("data", JSONObject.parse(data));
        }catch (Exception e){
            e.printStackTrace();
            result.put("returnCode", "FAIL");
        }
        return result;
    }

    @RequestMapping("/bookComments")
    public JSONObject bookComments(@RequestParam(value = "id") String id){
        JSONObject result = new JSONObject();
        try {
            String data = HttpClientUtils.sendGetData("http://api.zhuishushenqi.com/post/review/best-by-book?book="+id+"&limit=10", "utf-8");
            result.put("returnCode", "SUCCESS");
            result.put("data", JSONObject.parse(data));
        }catch (Exception e){
            e.printStackTrace();
            result.put("returnCode", "FAIL");
        }
        return result;
    }

    @RequestMapping("/bookSearchAutoComplete")
    public JSONObject bookSearchAutoComplete(@RequestParam(value = "keywords") String keywords){
        JSONObject result = new JSONObject();
        try {
            String data = HttpClientUtils.sendGetData("http://api.zhuishushenqi.com/book/auto-complete?query="+keywords, "utf-8");
            result.put("returnCode", "SUCCESS");
            result.put("data", JSONObject.parse(data));
        }catch (Exception e){
            e.printStackTrace();
            result.put("returnCode", "FAIL");
        }
        return result;
    }

    @RequestMapping("/bookSearch")
    public JSONObject bookSearch(@RequestParam(value = "keywords") String keywords){
        JSONObject result = new JSONObject();
        try {
            String data = HttpClientUtils.sendGetData("http://api.zhuishushenqi.com/book/fuzzy-search?query="+keywords+"&start=0&limit=20", "utf-8");
            result.put("returnCode", "SUCCESS");
            result.put("data", JSONObject.parse(data));
        }catch (Exception e){
            e.printStackTrace();
            result.put("returnCode", "FAIL");
        }
        return result;
    }

    @RequestMapping("/bookSource")
    public JSONObject bookSource(@RequestParam(value = "id") String id){
        JSONObject result = new JSONObject();
        try {
            String data = HttpClientUtils.sendGetData("http://api.zhuishushenqi.com/btoc?view=summary&book="+id, "utf-8");
            result.put("returnCode", "SUCCESS");
            result.put("data", JSONObject.parse(data));
        }catch (Exception e){
            e.printStackTrace();
            result.put("returnCode", "FAIL");
        }
        return result;
    }


    @RequestMapping("/bookChapterList")
    public JSONObject bookChapterList(@RequestParam(value = "id") String id){
        JSONObject result = new JSONObject();
        try {
            String sourceData = HttpClientUtils.sendGetData("http://api.zhuishushenqi.com/btoc?view=summary&book="+id, "utf-8");
            JSONArray sourceArray = JSONObject.parseArray(sourceData);
            if(null != sourceArray){
                JSONObject object = sourceArray.getJSONObject(0);
                if(object != null){
                    String _id = object.getString("_id");
                    System.out.println(_id);
                    String data = HttpClientUtils.sendGetData("http://api.zhuishushenqi.com/btoc/"+_id+"?view=chapters&channel=mweb", "utf-8");
                    result.put("returnCode", "SUCCESS");
                    result.put("data", JSONObject.parse(data));
                }else{
                    result.put("returnCode", "FAIL");
                }
            }else{
                result.put("returnCode", "FAIL");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.put("returnCode", "FAIL");
        }
        return result;
    }

    @RequestMapping("/bookChapterContent")
    public JSONObject bookChapterContent(@RequestParam(value = "link") String link){
        JSONObject result = new JSONObject();
        try {
            String data = HttpClientUtils.sendGetData("http://chapter2.zhuishushenqi.com/chapter/"+link+"?k=2124b73d7e2e1945&t=1468223717", "utf-8");
            result.put("returnCode", "SUCCESS");
            result.put("data", JSONObject.parse(data));
        }catch (Exception e){
            e.printStackTrace();
            result.put("returnCode", "FAIL");
        }
        return result;
    }

}
